package com.isoft.student.controller;

import com.isoft.student.eo.StudentEOPage;
import com.isoft.student.model.Student;
import com.isoft.student.service.StudentService;
import com.isoft.student.tool.ResponseMessage;
import com.isoft.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
     * 对外提供数据接口
     */
@Api(tags = "student")
@RestController
public class StudentController {
        @Autowired
        private StudentService studentService;

        @ApiOperation(value = "学生信息添加方法")
        @RequestMapping(value = "/addStudent", method = RequestMethod.GET)
        public int AddStudent(Student student) {
            int IsAdd = 0;
            //传入数据的判断
            //1.实体对象不能为空
            if (student != null) {
                //2.学生信息名称不能为空
                if (student.getSclass() != null) {
                    //3.添加学生信息
                    IsAdd = studentService.AddStudnt(student);
                }
            }
            return IsAdd;
        }

        @ApiOperation(value = "学生信息修改方法")
        @RequestMapping(value = "/updateStudent", method = RequestMethod.GET)
        public ResponseMessage updateStudent(Student student) {
            //传入的参数不能为空
            if (student != null) {
                //参数当中学生的学号不能为空
                if (student.getSid()!= 0) {
                    if (studentService.UpdateStudent(student) == 1) {
                        if (student.getSnumber() != null) {
                            if (student.getSclass() != null) {
                            }
                        }
                        return Result.success("更新成功");
                    } else {
                        return Result.error("更新失败");
                    }
                }
            }
                return Result.error("传入参数不正确");
            }

        @ApiOperation(value = "学生信息删除方法")
        @RequestMapping(value = "/delStudent", method = RequestMethod.GET)
        public ResponseMessage delStudent(int perid) {
            if (perid != 0) {
                int delInt = 0;
                delInt = studentService.DeleteStudent(perid);
                if (delInt > 0) {
                    return Result.success("删除成功");
                } else {
                    return Result.error("删除失败");
                }
            } else {
                return Result.error("删除的主键不能为0，请重新输入");
            }
        }

        @ApiOperation(value = "根据条件获取学生信息")
        @RequestMapping(value = "/getStudentWhereAll",produces = "application/json;charset=utf-8",method=RequestMethod.PUT)
        private ResponseMessage getAll(StudentEOPage studentEOPage){
            Map<String, List> map=new HashMap<>();
            List<StudentEOPage> listStudent=studentService.getStudentWhereAll(studentEOPage);
            map.put("list",listStudent);
            return Result.success("200",map);
        }
}

